//
//  ForgetViewController.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/20.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ForgetViewController.h"
#import "UIView+BackGround.h"
#import "TYMButton.h"
#import "LMJBaseResponse.h"
#import "UserService.h"
#import "Sizes.h"
#import "Colors.h"
#import "UIimage+Extends.h"
#import "WHToast.h"
#import "UIButton+Gradient.h"
#import "UIView+ZFFrame.h"
@interface ForgetViewController ()

@property(nonatomic,assign) NSInteger seconds;

@property(nonatomic,weak) IBOutlet UITextField  *phoneFid;
@property(nonatomic,weak) IBOutlet UITextField *nePassFid;
@property(nonatomic,weak) IBOutlet UITextField *codeFid;
@property(nonatomic,weak)IBOutlet TYMButton *codeBtn;
@property(nonatomic,weak) IBOutlet TYMButton *submitBtn;

@property(nonatomic,strong) UserService  *userService;
@end

@implementation ForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"忘记密码"];
    
    self.codeBtn.layer.cornerRadius = 3;
    self.codeBtn.layer.borderColor = rgba(74, 74, 74).CGColor;
    self.codeBtn.layer.borderWidth= 1.5;
    
    self.submitBtn.layer.cornerRadius = 5;
    self.submitBtn.layer.masksToBounds = YES;
    
    _seconds = 60;
    
    [self changedTextField:nil];
    [self.phoneFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.nePassFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    [self.codeFid addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    
}

-(void)changedTextField:(UITextField*)fid{
    if (self.phoneFid.text.length < 11 || self.nePassFid.text.length<1 || self.codeFid.text.length < 1) {
        [self.submitBtn setBackgroundImage:[UIImage imageWithColor:rgba(230, 230, 230) withSize:self.submitBtn.bounds.size] forState:UIControlStateNormal];
    }else{
        [self.submitBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
    }
}

-(IBAction)getCodeAction:(id)sender{
    
    if(_seconds < 60){
        return;
    }
    [self.userService getValidteionCode:self.phoneFid.text completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
        if (response.code == 0) {
            [self startCountDown];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

-(IBAction)changePasswordAction:(id)sender{
    TYMWeak(self);
    [self.userService getUpdatePassword:self.phoneFid.text validateionCode:self.codeFid.text newPassword:self.nePassFid.text completion:^(NSError * _Nonnull error, LMJBaseResponse * _Nonnull response) {
        if (response.code == 0) {
            [WHToast showMessage:@"密码修改成功" duration:2 finishHandler:^{
                [weakself.navigationController popViewControllerAnimated:YES];
            }];
        }else{
            [WHToast showMessage:response.message duration:2 finishHandler:nil];
        }
    }];
}

-(UserService*)userService{
    if (_userService == nil) {
        _userService = [[UserService alloc] init];
    }
    return _userService;;
}

//开始倒计时
-(void)startCountDown{
    [self changeSecend];
}

-(void)changeSecend{
    if (_seconds > 0) {
        _seconds =_seconds-1;
        [self.codeBtn setTitle:[NSString stringWithFormat:@"%lds",_seconds] forState:UIControlStateNormal];
        [self performSelector:@selector(changeSecend) withObject:self afterDelay:1];
    } else{
        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        _seconds = 60;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
